<?php

namespace App\Dao;

use App\Exception\ServiceException;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;

abstract class BaseDao
{
    /**
     * 新增
     * @param Model $model
     * @param array $data
     * @return Model
     */
    public function add(Model $model,array $data)
    {
        $result =  $model->fill($data)->save();

        if(!$result)
        {
            throw new ServiceException('创建失败！');
        }
        return $model;
    }

    /**
     * 更新
     * @param Model|Builder $model
     * @param array $data
     * @return Model
     */
    public function edit(Model $model,array $data)
    {
        $result = $model->fill($data)->update();
        if(!$result)
        {
            throw new ServiceException('更新失败！');
        }
        return $model;
    }
}